home *** CD-ROM | disk | FTP | other *** search
- Path: cscsun3.larc.nasa.gov!hook
- From: hook@cscsun3.larc.nasa.gov (Ed Hook)
- Newsgroups: comp.lang.c
- Subject: Re: sprintf() question
- Date: 29 Mar 1996 19:57:43 GMT
- Organization: CSC/NASA Langley Research Center
- Distribution: world
- Message-ID: <4jhfbn$p3@reznor.larc.nasa.gov>
- References: <31593522.76B3@cbm.com>
- Reply-To: hook@cscsun3.larc.nasa.gov
- NNTP-Posting-Host: cscsun3.larc.nasa.gov
-
- In article <31593522.76B3@cbm.com>, Dave Payne <paynedc@cbm.com> writes:
- |> Question on sprintf():
- |>
- |> Is it safe, and more importantly, is it ANSI standard, to use sprintf()
- |> to print into the same variable? Consider this example:
- |>
- |> char foo[100] = "foobar";
- |>
- |> sprintf(foo,"%s%s","the string is",foo);
- |>
- |> --------------------------------------------
- |>
- |> Would the resulting string be "the string is foobar", or will this
- |> code cause problems because I'm using the same variable (foo) to
- |> print to as well as read from?
-
- The Standard says that this invokes undefined behavior, so don't do it.
- Also, you could probably have answered your own question quite simply:
-
- poseidon2.hook 57 % cat oops.c
- #include <stdio.h>
-
- int main(void)
- {
- char foo[80+1+1];
-
- sprintf(foo,"initial foo");
- sprintf(foo,"the string is %s\n",foo);
- fputs(foo,stdout);
-
- return 0;
- }
-
- poseidon2.hook 58 % xlc -o oops oops.c
- poseidon2.hook 59 % oops
- the string is the string is
- poseidon2.hook 60 %
-
- See what I mean ?
-
- --
- Ed Hook | Coppula eam, se non posit
- Computer Sciences Corporation | acceptera jocularum.
- NASA Langley Research Center | Me? Speak for my employer?...<*snort*>
- Internet: hook@cscsun3.larc.nasa.gov | ... Get a _clue_ !!! ...
-